home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------
- #
- # NewsWatcher - Macintosh NNTP Client Application
- #
- # Written by Steven Falkenburg
- # ©1990 Apple Computer, Inc.
- #
- #-----------------------------------------------------------
- #
- # printstuff.c
- #
- # This module contains code which prints the contents of
- # a textedit record to a printer.
- #
- #-----------------------------------------------------------*/
-
- #include "compat.h"
-
- #ifdef PROTOS
- #include <Types.h>
- #include <Memory.h>
- #include <QuickDraw.h>
- #include <ToolUtils.h>
- #include <OSUtils.h>
- #include <Printing.h>
- #include <Lists.h>
- #endif
-
- #include "nntp.h"
- #include "printstuff.h"
- #include "miscstuff.h"
-
- #define odd(theInt) ( (theInt % 2) == 1 )
-
- extern TEHandle gFrontTE; /* frontmost textedit handle */
-
- static THPrint myHPrint; /* printing handle */
- static TEHandle printTE; /* textedit handle to print */
- static Handle txt; /* text to print */
- static short linesPerPage; /* # of lines per page */
-
- static TPPrPort prPort; /* printing port */
- static Boolean prIsOpen,docIsOpen,pageIsOpen; /* printing flags */
-
- void CleanUp(void)
- {
- MyIOCheck(PrError());
- if (pageIsOpen)
- PrClosePage(prPort);
- if (docIsOpen)
- PrCloseDoc(prPort);
- if (prIsOpen)
- PrClose();
- }
-
-
- OSErr ExecutePrint(THPrint hPrint)
- {
- short theFirst,theLast;
- Boolean wasnil,scratch;
- short nCopies;
- short prDevice;
- Boolean draftMode;
- TPrStatus prStatus;
- GrafPtr savePort;
- OSErr curPrError;
- Rect pageRect;
- short i,p;
-
- prIsOpen = docIsOpen = pageIsOpen = false;
- GetPort(&savePort);
- wasnil = (hPrint == nil);
- PrOpen();
- if (PrError() != noErr) {
- CleanUp();
- return(PrError());
- }
- prIsOpen = true;
- scratch = PrValidate(hPrint);
- if (!PrJobDialog(hPrint)) {
- CleanUp();
- return(PrError());
- }
- if (!MyPrepProc(hPrint)) {
- CleanUp();
- return(PrError());
- }
- prDevice = ( (**hPrint).prStl.wDev >> 8 );
- draftMode = !(odd((**hPrint).prJob.bJDocLoop));
- if ((draftMode) && (prDevice == 1))
- nCopies = (**hPrint).prJob.iCopies;
- else
- nCopies = 1;
-
- prPort = PrOpenDoc(hPrint,nil,nil);
- docIsOpen = true;
- curPrError = PrError();
- if (curPrError != noErr) {
- SetPort(savePort);
- return(curPrError);
- }
- SetPort(&prPort->gPort);
- for (i=1; i<=nCopies; i++) {
- theFirst = (**hPrint).prJob.iFstPage;
- theLast = (**hPrint).prJob.iLstPage;
- (**hPrint).prJob.iFstPage = 1;
- (**hPrint).prJob.iLstPage = 9999;
- for (p=theFirst; p<=theLast; p++) {
- PrOpenPage(prPort,nil);
- pageIsOpen = true;
- HLock((Handle)hPrint);
- pageRect = (**hPrint).prInfo.rPage;
- scratch = MyPageProc(hPrint,&pageRect,p);
- HUnlock((Handle)hPrint);
- PrClosePage(prPort);
- if (!scratch) {
- curPrError = iPrAbort;
- CleanUp();
- return(PrError());
- }
- }
- }
- PrCloseDoc(prPort);
- docIsOpen = false;
- curPrError = PrError();
-
- if ( !draftMode && (curPrError != noErr))
- PrPicFile(hPrint,nil,nil,nil,&prStatus);
- curPrError = PrError();
- PrClose();
- prIsOpen = false;
-
- return(noErr);
- }
-
-
- Boolean MyPrepProc(THPrint theHPrint)
- {
- short numPages,scrVert,scrHori;
- float docInches,printInches;
-
- ScreenRes(&scrVert,&scrHori);
- printInches = ((float) ( (**theHPrint).prInfo.rPage.bottom - (**theHPrint).prInfo.rPage.top)) / ((float)(**theHPrint).prInfo.iVRes);
- linesPerPage = (printInches / ( ((float)((**printTE).lineHeight)) / ((float) scrVert)) );
- if ((**theHPrint).prJob.iLstPage == 9999) {
- docInches = (**gFrontTE).lineHeight * (**gFrontTE).nLines / 72.0;
- numPages = docInches / printInches;
- if ( (docInches / printInches) != numPages )
- numPages++;
- (**theHPrint).prJob.iLstPage = numPages;
- }
- if ( (**theHPrint).prJob.iLstPage > 0 )
- return(true);
- else
- return(false);
- }
-
- Boolean MyPageProc(THPrint theHPrint,Rect *drawRect,short pageNum)
- {
- Ptr txtPtr;
- short start,length;
-
- #pragma unused (theHPrint)
-
- HLock(txt);
- TextFont((**printTE).txFont);
- TextFace((**printTE).txFace);
- TextMode((**printTE).txMode);
- TextSize((**printTE).txSize);
- start = (**printTE).lineStarts[((pageNum - 1) * linesPerPage)];
- if ((**printTE).nLines < (pageNum * linesPerPage))
- length = (**printTE).teLength - start;
- else
- length = (**printTE).lineStarts[(pageNum * linesPerPage)] - start;
- txtPtr = *txt + start;
- TextBox(txtPtr,length,drawRect,teJustLeft);
- HUnlock(txt);
- return(true);
- }
-
- void InitPrint(void)
- {
- PrOpen();
- MyIOCheck(PrError());
- myHPrint = (THPrint) MyNewHandle(sizeof(TPrint));
- if (MyMemErr() != noErr)
- return;
- PrintDefault(myHPrint);
- MyIOCheck(PrError());
- PrClose();
- }
-
- void DoPageSetup(void)
- {
- InitCursor();
- PrOpen();
- if (PrError() == noErr) {
- PrValidate(myHPrint);
- if (PrError() == noErr)
- PrStlDialog(myHPrint);
- else MyIOCheck(PrError());
- }
- else
- MyIOCheck(PrError());
- PrClose();
- MyIOCheck(PrError());
- }
-
- void DoPrint(void)
- {
- InitCursor();
- printTE = gFrontTE;
- if (MyHandToHand((Handle *)&printTE) != noErr)
- return;
- txt = (**gFrontTE).hText;
- if (MyHandToHand(&txt) != noErr)
- return;
- (**printTE).hText = txt;
- MyIOCheck( ExecutePrint(myHPrint) );
- MyDisposHandle(txt);
- MyDisposHandle((Handle)printTE);
- }
-